#include<iostream>
#include<unistd.h>
#include<cerrno>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include<stdlib.h>

using namespace std;


int main()
{
    int pipe_fd[2];
    int n = pipe(pipe_fd);
    if(n < 0)
    {
        cout << "pipe error:" << strerror(errno) << endl;
    }

    pid_t id = fork();
    if(id == 0)
    {
        close(pipe_fd[0]);
        char buff[] = "hello xiaohaohao";

        write(pipe_fd[1],buff,strlen(buff) );
        close(pipe_fd[1]);
        exit(0);
    }
    char out_buff[1024];
    close(pipe_fd[1]);
    
    read(pipe_fd[0],out_buff,1023);

    cout << "我是父进程，子进程写入：" << out_buff << endl;

    close(pipe_fd[0]);
    int status;
    waitpid(id, &status, 0);
    std::cout << "sig: " << (status & 0x7F) << std::endl;
    return 0;
}